﻿/**
 * ...
 * @author js
 */

package tab;

import flash.events.KeyboardEvent;
import flash.Lib;
import flash.text.TextField;
import flash.text.TextFieldType;
import gui.hug.HUGInputFieldSB;
import gui.hug.HUGSprite;
import gui.hug.HUGTabbedPanel;
import gui.hug.Tab;

import list.NewList;
import list.OpenTabEvent;

import loader.Loader;

import client.Channel;

class FindTag extends Tab
{
	private static inline var 	TITLE = "Поиск по категориям";
	
	private var loader 		: 	Loader;
	private var newsList 	: 	NewList;
	private var inp 		: TextField;
	private var sprite	    : HUGSprite;
	
	public function new(panel : HUGTabbedPanel = null, uid : Int) 
	{
	 Loader.createInstance();
	 loader = Loader.getInstance();
	 
	 sprite = new HUGSprite();	
	 inp = new TextField();
	 newsList = new NewList(0, 35, 600);
	
	 inp.x = inp.y = 10;
	 inp.height = 20;
	 inp.width = 400;
	 inp.type = TextFieldType.INPUT;
	 inp.border = true;
	 sprite.addChild(inp);
	 sprite.addChild(newsList);
	
	 Lib.current.addEventListener(KeyboardEvent.KEY_DOWN, hitEnter);
	
	 var onOpenTabEvent = function(e : OpenTabEvent) {
			var channel : Channel = cast(e.getContent(), Channel);
			var tab = new ItemsTab(channel.getChanId(), channel.getName(), panel, uid);
			tab.setId(channel.getChanId());
			panel.addTab(tab); 
		};
		
	 newsList.addEventListener(OpenTabEvent.OPEN_TAB, onOpenTabEvent);
	 super(sprite, TITLE,1, panel, true);
	}
	
    
    function hitEnter(event:KeyboardEvent)
    {
        if (event.keyCode == 13 && inp.text != "")
        {
			
			for (i in 0...newsList.getQuantity()){
			newsList.deleteItem(i); }
			
            loader.FindTag(onLoad, inp.text);
			
        }
    }
	
	public function onLoad(data : Array<Channel>) : Void
	{
		for (channel in data)
		{
			newsList.addItem(channel,0);
		}
	}	
	
}

